﻿using System;
using System.Linq;
using System.Data.Linq;
using UniMana.Data;
using UniMana.Utils.Configs;

namespace UniMana.Data
{
    public static class Log_ScoreEditData
    {
        #region [An]
        public static bool m_bFlag = false;

        /// <summary>
        /// Lấy dòng cuối trong bảng
        /// </summary>

        public static IQueryable GetLastRecord(this Table<Log_ScoreEdit> nLog_ScoreEdit)
        {
            var value = (from t in nLog_ScoreEdit
                         orderby
                          t.Log_ScoreEditKey descending
                         select new
                         {
                             t.ID
                         }).Take(1);
            return value;
        }

        /// <summary>
        /// Lấy tất cả dữ liệu
        /// </summary>
        /// <param name="scoreedit"></param>
        /// <returns></returns>

        public static IQueryable GetAllData(this Table<Log_ScoreEdit> nLog_ScoreEdit)
        {
            var value = from s in nLog_ScoreEdit
                        orderby
                            s.Log_ScoreEditKey descending
                        select s;
            return value;
        }

        /// <summary>
        /// Lấy các phiếu yêu cầu của 1 user
        /// </summary>
        /// <param name="scoreedit"></param>
        /// <param name="m_strUserKey"></param>
        /// <returns></returns>

        public static IQueryable GetDataByPerson(this Table<Log_ScoreEdit> nLog_ScoreEdit,
            string m_strUserKey)
        {
            var value = from s in nLog_ScoreEdit
                        where s.AccountRequire.ToString() == m_strUserKey
                        orderby
                            s.Log_ScoreEditKey descending
                        select s;
            return value;
        }

        /// <summary>
        /// Lấy danh sách phiếu yêu cầu sửa điểm để phê duyệt
        /// </summary>
        /// <param name="nLog_ScoreEdit"></param>
        /// <param name="nTraining_TrainingProgram"></param>
        /// <param name="nCatalogue_Major"></param>
        /// <param name="nPersonnel_Personnel"></param>
        /// <returns></returns>

        public static IQueryable GetListScoreEditForCensorship(this Table<Log_ScoreEdit> nLog_ScoreEdit,
            Table<Training_TrainingProgram> nTraining_TrainingProgram,
            Table<Catalogue_Major> nCatalogue_Major,
            Table<Personnel_Personnel> nPersonnel_Personnel)
        {
            var values = from t in nLog_ScoreEdit
                         join t1 in nTraining_TrainingProgram on t.Training_TrainingProgramKey equals t1.Training_TrainingProgramKey
                         join t2 in nCatalogue_Major on t1.Catalogue_MajorKey equals t2.Catalogue_MajorKey
                         join t3 in nPersonnel_Personnel on t.RepuiredBy equals t3.Personnel_PersonnelKey
                         select new
                         {
                             t.Log_ScoreEditKey,
                             t2.Catalogue_DepartmentKey,
                             t3.ID,
                             RequireUser = t.RepuiredBy,
                             t.Training_StudentKey,
                             t.Status
                         };
            return values.Distinct();
        }
        #endregion

        #region [Tan]

        public static bool Check(this Table<Log_ScoreEdit> t1, int subjectClassKey = 0, string student = UniMana.Utils.Configs.DefaultConfig.EMPTY_GUID)
        {
            var result = from l in t1
                         where l.Training_Subject_ClassKey == subjectClassKey
                         where l.Training_StudentKey == Guid.Parse(student)
                         select l;
            if (result.Count() > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public static IQueryable GetInfoEditScore(this Table<Log_ScoreEdit> t1, Table<Personnel_Personnel> t2,
            Table<Training_ScoreType> t3, int subjectClassKey = 0, string studentKey = DefaultConfig.EMPTY_GUID)
        {
            var result = from l in t1
                         join
                             p in t2 on l.RepuiredBy equals p.Personnel_PersonnelKey
                         join s in t3 on l.Training_ScoreTypeKey equals s.Training_ScoreTypeKey
                         where l.Training_StudentKey == Guid.Parse(studentKey)
                         select new
                         {
                             l.RepuiredBy,
                             l.Training_StudentKey,
                             l.Training_ScoreTypeKey,
                             ScoreType = s.Name,
                             l.OldScore,
                             l.Reasons,
                             FullNameRequired = p.LastName + " " + p.Name
                         };
            return result;
        }

        public static IQueryable GetList(this Table<Log_ScoreEdit> t1, int subjectClasKey = 0)
        {
            var s = from l in t1
                    where l.Training_Subject_ClassKey == subjectClasKey
                    select l;
            return s;
        }
        #endregion
    }
}
